{ "cells": [ { "cell_type": "markdown", "id": "ad441e7b", "metadata": {}, "source": [ "# Overriding a Mechanism in MusicBox\n", "\n", "Each MusicBox instance can only work with one mechanism at a time. To change the chemical system, you can load a new mechanism using the `load_mechanism()` method, which will replace the existing one. This tutorial demonstrates how to switch from one mechanism to a completely different chemical system." ] }, { "cell_type": "markdown", "id": "cad0ad50", "metadata": {}, "source": [ "## 1. Creating the Mechanism and Box Model\n", "\n", "This is simply a copy of the first half of the Basic Workflow Tutorial to set up the mechanism for overriding." ] }, { "cell_type": "code", "execution_count": 1, "id": "f0cb3f6f", "metadata": {}, "outputs": [], "source": [ "from acom_music_box import MusicBox\n", "import musica.mechanism_configuration as mc\n", "import matplotlib.pyplot as plt\n", "\n", "# Create species and phase for the reactions\n", "X = mc.Species(name=\"X\")\n", "Y = mc.Species(name=\"Y\")\n", "Z = mc.Species(name=\"Z\")\n", "species = {\"X\": X, \"Y\": Y, \"Z\": Z}\n", "gas = mc.Phase(name=\"gas\", species=list(species.values()))\n", "\n", "# Create the Arrhenius reactions\n", "reaction1 = mc.Arrhenius(name=\"X->Y\", A=4.0e-3, C=50, reactants=[species[\"X\"]], products=[species[\"Y\"]], gas_phase=gas)\n", "reaction2 = mc.Arrhenius(name=\"Y->Z\", A=4.0e-3, C=50, reactants=[species[\"Y\"]], products=[species[\"Z\"]], gas_phase=gas)\n", "rxns = {\"X->Y\": reaction1, \"Y->Z\": reaction2}\n", "\n", "# Create the mechanism that is defined by the species, phases, and reactions\n", "mechanism = mc.Mechanism(\n", " name=\"tutorial_mechanism\", \n", " species=list(species.values()), \n", " phases=[gas], \n", " reactions=list(rxns.values()))\n", "\n", "# Create the box model that contains the mechanism\n", "box_model = MusicBox()\n", "box_model.load_mechanism(mechanism)" ] }, { "cell_type": "markdown", "id": "017f8fa1", "metadata": {}, "source": [ "## 2. Overriding with a New Mechanism\n", "\n", "Now we'll completely replace the mechanism with a different chemical reactions. Instead of the Arrhenius reactions system, we'll create quantum tunneling reactions.\n", "\n", "This demonstrates how to override a mechanism by:\n", "- Using different species (G and H instead of X, Y, Z)\n", "- Using quantum tunneling reactions\n", "\n", "Here's the code to define and load the new mechanism:" ] }, { "cell_type": "code", "execution_count": 2, "id": "6c840133", "metadata": {}, "outputs": [], "source": [ "# Create new species and phase\n", "G = mc.Species(name=\"G\")\n", "H = mc.Species(name=\"H\")\n", "species = {\"G\": G, \"H\": H}\n", "\n", "gas = mc.Phase(name=\"gas\", species=list(species.values()))\n", "\n", "# Create the reactions and overwrite the variables\n", "reaction1 = mc.Tunneling(name=\"G->H\", A=2.0e-3, B=2, C=50, reactants=[species[\"G\"]], products=[species[\"H\"]], gas_phase=gas)\n", "reaction2 = mc.Tunneling(name=\"H->G\", A=9.0e-3, B=2, C=50, reactants=[species[\"H\"]], products=[species[\"G\"]], gas_phase=gas)\n", "rxns = {\"G->H\": reaction1, \"H->G\": reaction2}\n", "\n", "\n", "# Create the new mechanism with the newly defiend reactions\n", "mechanism = mc.Mechanism(\n", " name=\"new_mechanism\", \n", " species=list(species.values()), \n", " phases=[gas], \n", " reactions=list(rxns.values())\n", ")\n", "\n", "# Override the previous mechanism by loading the new one\n", "box_model.load_mechanism(mechanism)" ] }, { "cell_type": "markdown", "id": "62878d04", "metadata": {}, "source": [ "## 3. Running and Visualizing the New Box Model\n", "\n", "Now we can run the simulation with our new reversible mechanism. Notice that we're using the species names \"G\" and \"H\" instead of \"X\", \"Y\", and \"Z\" from the previous mechanism.\n", "\n", "The rest of this code is adapted from the Basic Workflow Tutorial. For detailed explanations of each step, refer to that tutorial:" ] }, { "cell_type": "code", "execution_count": 3, "id": "c2f1f497", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " " ] }, { "data": { "text/html": [ "
| \n", " | time.s | \n", "ENV.temperature.K | \n", "ENV.pressure.Pa | \n", "ENV.air number density.mol m-3 | \n", "CONC.G.mol m-3 | \n", "CONC.H.mol m-3 | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "0.0 | \n", "298.15 | \n", "101325.0 | \n", "40.874045 | \n", "2.500000 | \n", "5.000000 | \n", "
| 1 | \n", "20.0 | \n", "298.15 | \n", "101325.0 | \n", "40.874045 | \n", "3.213819 | \n", "4.286181 | \n", "
| 2 | \n", "40.0 | \n", "298.15 | \n", "101325.0 | \n", "40.874045 | \n", "3.787516 | \n", "3.712484 | \n", "
| 3 | \n", "60.0 | \n", "298.15 | \n", "101325.0 | \n", "40.874045 | \n", "4.248595 | \n", "3.251405 | \n", "
| 4 | \n", "80.0 | \n", "298.15 | \n", "101325.0 | \n", "40.874045 | \n", "4.619165 | \n", "2.880835 | \n", "
| 5 | \n", "100.0 | \n", "298.15 | \n", "101325.0 | \n", "40.874045 | \n", "4.916991 | \n", "2.583009 | \n", "
| 6 | \n", "120.0 | \n", "310.00 | \n", "100100.0 | \n", "38.836331 | \n", "5.156409 | \n", "2.343591 | \n", "
| 7 | \n", "140.0 | \n", "310.00 | \n", "100100.0 | \n", "38.836331 | \n", "5.348819 | \n", "2.151181 | \n", "
| 8 | \n", "160.0 | \n", "310.00 | \n", "100100.0 | \n", "38.836331 | \n", "5.503449 | \n", "1.996551 | \n", "
| 9 | \n", "180.0 | \n", "310.00 | \n", "100100.0 | \n", "38.836331 | \n", "5.627719 | \n", "1.872281 | \n", "
| 10 | \n", "200.0 | \n", "310.00 | \n", "100100.0 | \n", "38.836331 | \n", "5.727589 | \n", "1.772411 | \n", "